import { useTitle } from '@vueuse/core';
import { createRouter, createWebHashHistory } from 'vue-router';
import useRouteStore from '@/store/modules/route';
import { navigationConfig } from '@/utils/bridge';
import type { EnhancedRouteLocation } from '@/typings/router.d';
import routes from './routes';

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach((to: EnhancedRouteLocation, from, next) => {
  const routeStore = useRouteStore();
  routeStore.setCacheRoutes(to);
  next();
});

router.beforeResolve(to => {
  const title = to.meta.title as string || '';
  useTitle(title);
  navigationConfig({
    title,
    showNavBar: to?.meta?.showNavBar === false ? false : true
  });
})

export default router;
